package com.ssd.controller;

import com.ssd.entity.VendingMachineEntity;
import com.ssd.service.VMService;
import com.ssd.utils.StringUtils;
import com.ssd.view.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/vm")
@Slf4j
public class VMController {

    @Resource
    private VMService vmService;

    // 分页查询商品列表
    @GetMapping("/list")
    public BaseResponse<List<VendingMachineEntity>> getVMPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String innerCode) {
        return new BaseResponse(vmService.getVMList(pageNum, pageSize ,innerCode));
    }

    /**
     * 根据主键获取商品详情
     */
    @GetMapping("/{vmId}")
    public BaseResponse<VendingMachineEntity> getVMDetail(@PathVariable("vmId") Long vmId) {
        return new BaseResponse(vmService.selectVMByVMId(vmId));
    }

    /**
     * 新增商品信息
     */
    @PostMapping
    public BaseResponse<String> addVM(@RequestBody VendingMachineEntity vm) {
        return new BaseResponse(vmService.insertVM(vm));
    }

    /**
     * 修改商品信息
     */
    @PutMapping
    public BaseResponse<String> updateVM(@RequestBody VendingMachineEntity vm) {
        return new BaseResponse(vmService.updateVM(vm));
    }

    /**
     * 批量删除商品信息
     */
    @DeleteMapping("/{vmIds}")
    public BaseResponse<String> batchDeleteVM(@PathVariable("vmIds") String classIdsStr) {
        Long[] classIds = StringUtils.isEmpty(classIdsStr) ?
                new Long[0] :
                Arrays.stream(classIdsStr.split(","))
                        .map(Long::parseLong)
                        .toArray(Long[]::new);
        return new BaseResponse(vmService.deleteVMByVMIds(classIds));
    }
}